//
//  XJSetLoginPasswdViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/12.
//  Copyright © 2018年 Tzyang. All rights reserved.
//


#import "XJSetLoginPasswdViewController.h"

// View
#import "ZYTextField.h"
#import "XJCodeView.h"

@interface XJSetLoginPasswdViewController ()<XJCodeDelegate>

@property(nonatomic,strong)  UIView *nextBgView;//下一步view
@property(nonatomic, strong) ZYTextField *phonteTF;
@property(nonatomic, strong) ZYTextField *codeTF;
@property(nonatomic, strong) XJCodeView *codeView;
@property(nonatomic, strong) UIButton *nextButton;


@property(nonatomic,strong)  UIView *sureBgView; //确认修改veiw
@property(nonatomic, strong) ZYTextField *pwdTF;
@property(nonatomic, strong) ZYTextField *surePwdTF;
@property(nonatomic, strong) UIButton *sureButton;

@end

@implementation XJSetLoginPasswdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"登录密码";
    
    [self setupView];
}

- (void)setupView{
    
    [self.view addSubview:self.nextBgView];
    [self.view addSubview:self.sureBgView]; // 默认hidden= YES
    
}

#pragma mark - 一 下一步view
- (ZYTextField *)phonteTF {
    if (!_phonteTF) {
        _phonteTF = [[ZYTextField alloc] initWithFrame:CGRectMake(0, 0, self.nextBgView.frame.size.width, 50) placeholder:@"请输入手机号" keyboardType:(UIKeyboardTypePhonePad) leftViewImageName:@"login_phone" textEntry:NO];
        
        [_phonteTF addTarget:self action:@selector(modifyPasswrod_phonChnaged:) forControlEvents:(UIControlEventEditingChanged)];

    }
    return _phonteTF;
}
- (ZYTextField *)codeTF {
    if (!_codeTF) {
        _codeTF = [[ZYTextField alloc] initWithFrame:CGRectMake(0, 50, self.nextBgView.frame.size.width, 50) placeholder:@"请输入验证码" keyboardType:(UIKeyboardTypePhonePad) leftViewImageName:@"login_verify" textEntry:NO];
        
        //发送验证码view
        UIView *rightView  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
        [rightView addSubview:self.codeView];
        
        _codeTF.rightView = rightView;
        _codeTF.rightViewMode = UITextFieldViewModeAlways;
        
    }
    return _codeTF;
}
- (XJCodeView *)codeView {
    if (!_codeView) {
        _codeView = [[XJCodeView alloc] initWithTime:60 frame:CGRectMake(0, 10, 50, 30)];
        _codeView.codeBtn.userInteractionEnabled = NO;
        _codeView.codeBtn.backgroundColor  = MAINS_COLOR;
        _codeView.delegate = self;
    }
    return _codeView;
}
- (UIButton *)nextButton {
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _nextButton.layer.masksToBounds = YES;
        _nextButton.layer.cornerRadius = 10;
        [_nextButton setTitle:@"下一步" forState:(UIControlStateNormal)];
        [_nextButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _nextButton.backgroundColor = XJCOLOR_RGB(255, 216, 68);
        _nextButton.frame = CGRectMake(0, self.nextBgView.frame.size.height - 100, self.nextBgView.frame.size.width, 50);
        [_nextButton addTarget:self action:@selector(nextButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _nextButton;
}


- (UIView *)nextBgView {
    if (!_nextBgView) {
        _nextBgView = [[UIView alloc] initWithFrame:CGRectMake(15,64 , KWidth - 30, 300)];
        
        // 手机号
        [_nextBgView addSubview:self.phonteTF];
        // 验证码
        [_nextBgView addSubview:self.codeTF];
        
        // 下一步按钮
        [_nextBgView addSubview:self.nextButton];
    }
    return _nextBgView;
}
#pragma mark - 点击事件
- (void)getCheckCode:(NSString *)checkCode {
    NSString *phone = self.phonteTF.text;
    if ([NSString isEmptyString:phone]) {
        [JRToast showWithText:@"请输入手机号"];
        return;
    }
    // 网络请求：修改登录密码
    [XJHttpTool requestSendCodeWithPhone:phone type:@"editpwdlogin" successH:^(id responseObjc) {
        XJLog(@"%@",responseObjc);
        NSNumber *code = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
            [JRToast showWithText:@"验证码已发送"];
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
    } failureH:^(NSError *error) {
        XJLog(@"%@",error);
        [JRToast showWithText:[error localizedDescription]];

    }];
}

- (void)modifyPasswrod_phonChnaged:(ZYTextField *)phoneTextField {
    XJLog(@"");
    
    NSString *phone = phoneTextField.text;
    if ([phone isPhoneNumber]) {
        self.codeView.codeBtn.userInteractionEnabled = YES;
        self.codeView.codeBtn.backgroundColor = MAIN_COLOR;
    }else {
        self.codeView.codeBtn.userInteractionEnabled = NO;
        self.codeView.codeBtn.backgroundColor = MAINS_COLOR;
    }
}

- (void)nextButtonAction{
    XJLog(@"");
    
    NSString *phone = self.phonteTF.text;
    NSString *codeStr = self.codeTF.text;
    if ([NSString isEmptyString:phone]) {
        [JRToast showWithText:@"请输入手机号"];
        return;
    }
    if ([NSString isEmptyString:codeStr]) {
        [JRToast showWithText:@"请输入验证码"];
        return;
    }
    // 调接口
    [XJHttpTool requestVerifyCodeWithPhone:phone code:codeStr type:@"editpwdlogin" successH:^(id responseObjc) {
        
        // 成功控制hidden
        NSNumber *code = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
        
            self.nextBgView.hidden = YES;
            self.sureBgView.hidden = NO;
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }

    } failureH:^(NSError *error) {
//        XJLog(@"%@",error);
        [JRToast showWithText:[error localizedDescription]];
    }];
}


#pragma mark - 二 确定view
- (ZYTextField *)pwdTF {
    if (!_pwdTF) {
        _pwdTF = [[ZYTextField alloc] initWithFrame:CGRectMake(15, 0, KWidth - 30, 50) placeholder:@"请输入新密码 6-20位" keyboardType:(UIKeyboardTypePhonePad) leftViewImageName:@"login_password" textEntry:YES];
    }
    return _pwdTF;
}
- (ZYTextField *)surePwdTF {
    if (!_surePwdTF) {
        _surePwdTF = [[ZYTextField alloc] initWithFrame:CGRectMake(15, 50, KWidth - 30, 50) placeholder:@"请输入新密码 6-20位" keyboardType:(UIKeyboardTypePhonePad) leftViewImageName:@"login_password" textEntry:YES];
    }
    return _surePwdTF;
}
- (UIButton *)sureButton {
    if (!_sureButton) {
        _sureButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _sureButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _sureButton.layer.masksToBounds = YES;
        _sureButton.layer.cornerRadius = 10;
        [_sureButton setTitle:@"确认修改" forState:(UIControlStateNormal)];
        [_sureButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _sureButton.backgroundColor = XJCOLOR_RGB(255, 216, 68);
        
        _sureButton.frame = CGRectMake(0, self.nextBgView.frame.size.height - 100, self.nextBgView.frame.size.width, 50);

        [_sureButton addTarget:self action:@selector(sureButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _sureButton;
}


- (UIView *)sureBgView {
    if (!_sureBgView) {
        _sureBgView = [[UIView alloc] initWithFrame:CGRectMake(15, 64, KWidth - 30, 300)];
        _sureBgView.hidden = YES;
        
        //新密码
        [_sureBgView addSubview:self.pwdTF];
        // 新确认密码
        [_sureBgView addSubview:self.surePwdTF];
        // 确定修改button
        [_sureBgView addSubview:self.sureButton];
    }
    return _sureBgView;
}

#pragma mark - 点击事件
- (void)sureButtonAction {
    // 调接口
    
    NSString *pwd = self.pwdTF.text;
    NSString *surePwd = self.surePwdTF.text;
    
    if ([NSString isEmptyString:pwd]) {
        [JRToast showWithText:@"请输入新密码"];
        return;
    }
    if ([NSString isEmptyString:surePwd]) {
        [JRToast showWithText:@"请输入确认密码"];
        return;
    }
    
    if (![pwd isEqualToString:surePwd]) {
        [JRToast showWithText:@"两次输入密码不一致"];
        return;
    }
    
    // 密码MD5加密并转小写传递过去
    NSString *md5pwd = [NSString md5:pwd];
    md5pwd = md5pwd.lowercaseString;
    
    
    [XJHttpTool requestModifyLoginPwd:md5pwd successH:^(id responseObjc) {
        XJLog(@"%@",responseObjc);
        
        NSNumber * code = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
            [JRToast showWithText:@"修改成功"];
            
            //返回
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
        
    } failH:^(NSError *error) {
        XJLog(@"%@",error);
        [JRToast showWithText:[error localizedDescription]];
    }];
}



@end
